package com;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.hzz.framework.util.CommonConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@Order(22)
@EnableSwagger2
public class SwaggerConfig {
    @Value("${common.app-prefix}")
    String appPrefix;
    @Value("${info.description}")
    String description;
    @Value("${info.version}")
    String version;

    @Bean
    public Docket testApi() {
//        if (!"dev_".equals(appPrefix)) {
//            return new Docket(DocumentationType.SWAGGER_2).enable(false);
//        }
        //设置请求头
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name(CommonConstant.JWT_HEADER_TOKEN_KEY).description("签名授权")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("hzz")
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// api测试请求地址
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hzz"))//com.hzz.framework.fileServer
                .paths(Predicates.or(new Predicate[]{
                        PathSelectors.regex("/api/.*"),
                        PathSelectors.regex("/area/.*"),
                        PathSelectors.regex("/file/.*")
                }))// 过滤的接口
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(Date.class, java.util.Date.class)
                .apiInfo(apiInfo())
                .globalOperationParameters(parameters);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("APIs")
                .description("" + description)
                .version("" + version)
                .build();
    }
}
